﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Inv;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageReturnImportOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Vars
        private InvReturnImportOrderEntity mSelectedReturnImportOrder;

        #endregion

        #region Cons
        public frmManageReturnImportOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Func
        private void bindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "IRIO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "IRIO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUnit, "UNIT", true);
        }

        private string GetCondition()
        {
            string condtion = "";
            if (!string.IsNullOrEmpty(txtOrderCode.Text))
            {
                condtion += @" AND A.ORDER_CODE LIKE '%" + txtOrderCode.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtReturnCode.Text))
            {
                condtion += @" AND A.RETURN_ORDER_CODE LIKE '%"+txtReturnCode.Text+"%'";

            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condtion += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }

            return condtion;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ORDER_CODE DESC";
            DataTable dt = InvReturnImportOrderBs.GetByConditon(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BindDetail()
        {
            if (mSelectedReturnImportOrder == null)
            {
                return;
            }
            DataTable dt = InvReturnImportItemBs.GetInvReturnImportItemByOrderId(mSelectedReturnImportOrder.Id);
            if (dt == null || dt.Rows.Count == 0)
            {
                gvDetail.DataSource = null;
                return;
            }
            gvDetail.DataSource = dt;
        }
        private void ResetButtonEnable()
        {
            btnNew.Enabled = false;
            btnModify.Enabled = false;
            btnApprove.Enabled = false;
            btnDelete.Enabled = false;
            if (mSelectedReturnImportOrder.Status == "IRIO01"||mSelectedReturnImportOrder.Status=="IRIO02")
            {
                btnModify.Enabled = true;
                btnApprove.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedReturnImportOrder.Status == "IRIO03")
            {
 
            }
            else if (mSelectedReturnImportOrder.Status == "IRIO04")
            {
                btnDelete.Enabled = true;
            }
            
        }
        #endregion

        private void frmManageReturnImportOrder_Load(object sender, EventArgs e)
        {
            bindCombobox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedReturnImportOrder = new InvReturnImportOrderEntity(dr);
            BindDetail();
            ResetButtonEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_Modify()
        {
            if (mSelectedReturnImportOrder == null)
            {
                MessageBox.Show("请选择一个退货入库单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnImportOrder frm = new frmMaintainReturnImportOrder();
            frm.ThisActionType = ActionType.Modify;
            frm.selectedReturnImportOrder = mSelectedReturnImportOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedReturnImportOrder == null)
            {
                MessageBox.Show("请选择一个退货入库单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnImportOrder frm = new frmMaintainReturnImportOrder();
            frm.ThisActionType = ActionType.Approve;
            frm.selectedReturnImportOrder = mSelectedReturnImportOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectedReturnImportOrder == null)
            {
                MessageBox.Show("请选择一个退货入库单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确认删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReturnImportOrder.Status = "IRIO05";
                mSelectedReturnImportOrder.Modified = DateTime.Now;
                mSelectedReturnImportOrder.ModifiedBy = CommonParameter.CurrentUser.PersonId;
                bool isSuccessful = InvReturnImportOrderBs.UpdateInvReturnImportOrder(mSelectedReturnImportOrder);
                if (isSuccessful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        protected override void Click_View()
        {
            if (mSelectedReturnImportOrder == null)
            {
                MessageBox.Show("请选择一个退货入库单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnImportOrder frm = new frmMaintainReturnImportOrder();
            frm.ThisActionType = ActionType.View;
            frm.selectedReturnImportOrder = mSelectedReturnImportOrder;
            frm.ShowDialog();
        }
    }
}